/**
 * $Id: CNGPCommon.java,v 1.1 2006/09/22 07:56:44 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.cngpmsg;

public class CNGPCommon {
    public static final int CNGP_LOGIN = 0x00000001;
    public static final int CNGP_LOGIN_RESP = 0x80000001;
    public static final int CNGP_SUBMIT = 0x00000002;
    public static final int CNGP_SUBMIT_RESP = 0x80000002;
    public static final int CNGP_DELIVER = 0x00000003;
    public static final int CNGP_DELIVER_RESP = 0x80000003;
    public static final int CNGP_ACTIVE = 0x00000004;
    public static final int CNGP_ACTIVE_RESP = 0x80000004;
    public static final int CNGP_EXIT = 0x00000006;
    public static final int CNGP_EXIT_RESP = 0x80000006;

    public static String getNetMsgId(byte[] bytes) {
        byte[] tmpid = new byte[3];
        System.arraycopy(bytes, 0, tmpid, 0, 3);
        StringBuffer buff = new StringBuffer(bcd2Str(tmpid));
        tmpid = new byte[4];
        System.arraycopy(bytes, 3, tmpid, 0, 4);
        buff.append(bcd2Str(tmpid));
        tmpid = new byte[3];
        System.arraycopy(bytes, 7, tmpid, 0, 3);
        buff.append(bcd2Str(tmpid));
        return buff.toString();

    }

    public static String bcd2Str(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
                .toString().substring(1) : temp.toString();
    }
}
